package middleware

import (
	"gin_gorm_oj/help"
	"github.com/gin-gonic/gin"
)

func AuthUserCheck() gin.HandlerFunc {
	return func(c *gin.Context) {
		//check if user
		auth := c.GetHeader("Authorization")
		userClaim, err := help.AnalyseToken(auth)
		c.Set("user", userClaim)
		if err != nil {
			c.Abort()
			c.JSON(200, gin.H{
				"code": 401,
				"msg":  "登录错误",
			})
			return
		}
		if userClaim == nil || userClaim.IsAdmin != 1 {
			c.Abort()
			c.JSON(200, gin.H{
				"code": 401,
				"msg":  "不是管理员",
			})
			return
		}
		c.Next()

	}
}
